package com.cn.springcloudshop.common;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebMvcStaticConfig implements WebMvcConfigurer {
	@Autowired
	ServletContext servletContext;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 将所有/static/** 访问都映射到classpath:/static/ 目录下
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("/img/**").addResourceLocations("classpath:templates/img/");
		registry.addResourceHandler("/assets/**").addResourceLocations("classpath:templates/assets/");
		registry.addResourceHandler("/html/**").addResourceLocations("classpath:templates/html/");
		registry.addResourceHandler("/js/**").addResourceLocations("classpath:static/js/");
		registry.addResourceHandler("/css/**").addResourceLocations("classpath:static/css/");
		//registry.addResourceHandler("/system/**").addResourceLocations("file:webapp/WEB-INF/views/system/");
		registry.addResourceHandler("/**").addResourceLocations("/");
	}
	@Bean
	FreeMarkerConfigurer FreeMarkerConfigurer(){
		FreeMarkerConfigurer freeMarkerConfigurer=new FreeMarkerConfigurer();
		freeMarkerConfigurer.setTemplateLoaderPath("classpath:/templates/");
		freeMarkerConfigurer.setDefaultEncoding("UTF-8");
		return freeMarkerConfigurer;
	}
	@Bean(name = "htmlviewResolver")
	FreeMarkerViewResolver htmlviewResolver(){
		FreeMarkerViewResolver freeMarkerViewResolver=new FreeMarkerViewResolver();
		freeMarkerViewResolver.setSuffix(".html");
		freeMarkerViewResolver.setOrder(0);
		freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
		freeMarkerViewResolver.setCache(false);
		return freeMarkerViewResolver;
	}


	/**
	 * 文件上传大小
	 * @return
	 */
	@Bean
	MultipartConfigElement multipartConfigElement(){
		MultipartConfigFactory factory = new MultipartConfigFactory();
		//文件最大
		factory.setMaxFileSize("10240MB"); //KB,MB
		/// 设置总上传数据总大小
		factory.setMaxRequestSize("102400MB");
		return factory.createMultipartConfig();
	}


	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		configurer.setUseSuffixPatternMatch(true).setUseTrailingSlashMatch(true);
	}

//	/**
//	 * 文件上传配置
//	 *
//	 * @return
//	 */
//	@Bean
//	public MultipartConfigElement multipartConfigElement() {
//		MultipartConfigFactory factory = new MultipartConfigFactory();
//		// 文件最大
//		factory.setMaxFileSize("1024MB"); // KB,MB
//		// / 设置总上传数据总大小
//		factory.setMaxRequestSize("1024MB");
//		return factory.createMultipartConfig();
//	}
}